package com.liujiaqi.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liujiaqi.lease.model.entity.*;
import com.liujiaqi.lease.model.enums.ItemType;
import com.liujiaqi.lease.web.admin.mapper.*;
import com.liujiaqi.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujiaqi.lease.web.admin.vo.attr.AttrValueVo;
import com.liujiaqi.lease.web.admin.vo.graph.GraphVo;
import com.liujiaqi.lease.web.admin.vo.room.RoomDetailVo;
import com.liujiaqi.lease.web.admin.vo.room.RoomItemVo;
import com.liujiaqi.lease.web.admin.vo.room.RoomQueryVo;
import com.liujiaqi.lease.web.admin.vo.room.RoomSubmitVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

  @Autowired
  private RoomInfoMapper roomInfoMapper;

  @Autowired
  private GraphInfoService graphInfoService;
  
  @Autowired
  private RoomAttrValueService roomAttrValueService;

  @Autowired
  private RoomFacilityService roomFacilityService;

  @Autowired
  private RoomLabelService roomLabelService;

  @Autowired
  private RoomPaymentTypeService roomPaymentTypeService;

  @Autowired
  private RoomLeaseTermService roomLeaseTermService;

  @Autowired
  private ApartmentInfoService apartmentInfoService;

  @Autowired
  private GraphInfoMapper graphInfoMapper;

  @Autowired
  private AttrValueMapper attrValueMapper;

  @Autowired
  private FacilityInfoMapper facilityInfoMapper;

  @Autowired
  private LabelInfoMapper labelInfoMapper;

  @Autowired
  private PaymentTypeMapper paymentTypeMapper;

  @Autowired
  private LeaseTermMapper leaseTermMapper;

  @Override
  public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {

    // 判断是否存在
    boolean isUpdate = roomSubmitVo.getId() != null;

    // 更新基础信息
    super.saveOrUpdate(roomSubmitVo);

    if (isUpdate) {
      // 1.删除图片信息列表
      LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
      graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
      graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, roomSubmitVo.getId());
      graphInfoService.remove(graphInfoLambdaQueryWrapper);

      // 2.删除属性信息列表
      LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
      roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, roomSubmitVo.getId());
      roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

      // 3.删除配置信息列表
      LambdaQueryWrapper<RoomFacility> roomFacilityLambdaUpdateWrapper = new LambdaQueryWrapper<>();
      roomFacilityLambdaUpdateWrapper.eq(RoomFacility::getRoomId, roomSubmitVo.getId());
      roomFacilityService.remove(roomFacilityLambdaUpdateWrapper);

      // 4.删除标签信息列表
      LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
      roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, roomSubmitVo.getId());
      roomLabelService.remove(roomLabelLambdaQueryWrapper);

      // 5.删除支付方式列表
      LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
      roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, roomSubmitVo.getId());
      roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

      // 6.删除可选租期列表
      LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
      roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, roomSubmitVo.getId());
      roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }

    // 1.插入图片信息列表
    List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
    if(!CollectionUtils.isEmpty(graphVoList)) {
      ArrayList<GraphInfo> graphInfoList = new ArrayList<>();
      for (GraphVo graphVo : graphVoList) {
        GraphInfo graphInfo = new GraphInfo();
        graphInfo.setItemId(roomSubmitVo.getId());
        graphInfo.setItemType(ItemType.ROOM);
        graphInfo.setUrl(graphVo.getUrl());
        graphInfo.setName(graphVo.getName());
        graphInfoList.add(graphInfo);
      }
      graphInfoService.saveBatch(graphInfoList);
    }

    // 2.插入属性信息列表
    List<Long> attrValueIdList = roomSubmitVo.getAttrValueIds();
    if(!CollectionUtils.isEmpty(attrValueIdList)) {
      ArrayList<RoomAttrValue> roomAttrValueList = new ArrayList<>();
      for (Long attrValueId : attrValueIdList) {
        RoomAttrValue roomAttrValue = new RoomAttrValue();
        roomAttrValue.setRoomId(roomSubmitVo.getId());
        roomAttrValue.setAttrValueId(attrValueId);
        roomAttrValueList.add(roomAttrValue);
      }
      roomAttrValueService.saveBatch(roomAttrValueList);
    }

    // 3.插入配置信息列表
    List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
    if(!CollectionUtils.isEmpty(facilityInfoIds)) {
      ArrayList<RoomFacility> roomFacilityList = new ArrayList<>();
      for (Long facilityInfoId : facilityInfoIds) {
        RoomFacility roomFacility = new RoomFacility();
        roomFacility.setRoomId(roomSubmitVo.getId());
        roomFacility.setFacilityId(facilityInfoId);
        roomFacilityList.add(roomFacility);
      }
      roomFacilityService.saveBatch(roomFacilityList);
    }

    // 4.插入标签信息列表
    List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
    if(!CollectionUtils.isEmpty(labelInfoIds)) {
      ArrayList<RoomLabel> roomLabelList = new ArrayList<>();
      for (Long labelInfoId : labelInfoIds) {
        RoomLabel roomLabel = new RoomLabel();
        roomLabel.setRoomId(roomSubmitVo.getId());
        roomLabel.setLabelId(labelInfoId);
        roomLabelList.add(roomLabel);
      }
      roomLabelService.saveBatch(roomLabelList);
    }

    // 5.插入支付方式列表
    List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
    if(!CollectionUtils.isEmpty(paymentTypeIds)) {
      ArrayList<RoomPaymentType> roomPaymentTypeList = new ArrayList<>();
      for (Long paymentTypeId : paymentTypeIds) {
        RoomPaymentType roomPaymentType = new RoomPaymentType();
        roomPaymentType.setRoomId(roomSubmitVo.getId());
        roomPaymentType.setPaymentTypeId(paymentTypeId);
        roomPaymentTypeList.add(roomPaymentType);
      }
      roomPaymentTypeService.saveBatch(roomPaymentTypeList);
    }

    // 6.插入可选租期列表
    List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
    if(!CollectionUtils.isEmpty(leaseTermIds)) {
      ArrayList<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>();
      for (Long leaseTermId : leaseTermIds) {
        RoomLeaseTerm roomLeaseTerm = new RoomLeaseTerm();
        roomLeaseTerm.setRoomId(roomSubmitVo.getId());
        roomLeaseTerm.setLeaseTermId(leaseTermId);
        roomLeaseTermList.add(roomLeaseTerm);
      }
      roomLeaseTermService.saveBatch(roomLeaseTermList);
    }
  }

  @Override
  public IPage<RoomItemVo> pageRoomItemByQuery(Page<RoomItemVo> page, RoomQueryVo queryVo) {
    return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
  }

  @Override
  public RoomDetailVo getDetailById(Long id) {

    RoomInfo roomInfo = super.getById(id);

    // 1.获取图片信息列表
    List<GraphVo> graphVoList = graphInfoMapper.selectListByItemTypeAndId(ItemType.ROOM, id);

    // 2.获取属性信息列表
    List<AttrValueVo> attrValueVoList = attrValueMapper.selectByRoomId(id);

    // 3.获取配置信息列表
    List<FacilityInfo> facilityInfoList = facilityInfoMapper.selectByRoomId(id);

    // 4.获取标签信息列表
    List<LabelInfo> labelInfoList = labelInfoMapper.selectListByRoomId(id);

    // 5.获取支付方式列表
    List<PaymentType> paymentTypeList = paymentTypeMapper.selectByRoomId(id);

    // 6.获取可选租期列表
    List<LeaseTerm> leaseTermList = leaseTermMapper.selectByRoomId(id);

    // 7.获取公寓信息
    ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());

    RoomDetailVo roomDetailVo = new RoomDetailVo();

    BeanUtils.copyProperties(roomInfo, roomDetailVo);
    roomDetailVo.setApartmentInfo(apartmentInfo);

    roomDetailVo.setAttrValueVoList(attrValueVoList);
    roomDetailVo.setGraphVoList(graphVoList);
    roomDetailVo.setFacilityInfoList(facilityInfoList);
    roomDetailVo.setLabelInfoList(labelInfoList);
    roomDetailVo.setPaymentTypeList(paymentTypeList);
    roomDetailVo.setLeaseTermList(leaseTermList);

    return roomDetailVo;
  }

  @Override
  public void removeRoomById(Long id) {
    // 1.删除图片信息列表
    LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
    graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
    graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId, id);
    graphInfoService.remove(graphInfoLambdaQueryWrapper);

    // 2.删除属性信息列表
    LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
    roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId, id);
    roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

    // 3.删除配置信息列表
    LambdaQueryWrapper<RoomFacility> roomFacilityLambdaUpdateWrapper = new LambdaQueryWrapper<>();
    roomFacilityLambdaUpdateWrapper.eq(RoomFacility::getRoomId, id);
    roomFacilityService.remove(roomFacilityLambdaUpdateWrapper);

    // 4.删除标签信息列表
    LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
    roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId, id);
    roomLabelService.remove(roomLabelLambdaQueryWrapper);

    // 5.删除支付方式列表
    LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
    roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId, id);
    roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

    // 6.删除可选租期列表
    LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
    roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId, id);
    roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

    super.removeById(id);
  }
}




